package cn.wolfcode.web.controller;



import cn.wolfcode.domain.Business;
import cn.wolfcode.enums.BusinessReportEnums;
import cn.wolfcode.mapper.BusinessReportMapper;
import cn.wolfcode.qo.BusinessReportQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {

    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;

    @RequiredPermission(name="门店报表页面",expression="businessReport:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo){
        //门店
        model.addAttribute("businesses",businessService.listAll());
        //枚举
        model.addAttribute("enums", BusinessReportEnums.values());
        PageInfo<HashMap> pageInfo = businessReportService.report(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "businessReport/list";
    }


    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo){
        //查询数据,不分页
        qo.setPageSize(0);
        PageInfo<HashMap> report = businessReportService.report(qo);
        List<HashMap> list = report.getList();
        //准备5个容器
        ArrayList<Object> XList = new ArrayList<>();
        ArrayList<Object> numList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();
        //for循环遍历list
        for (HashMap map : list) {
            XList.add(map.get("groupType"));
            numList.add(map.get("num"));
            totalAmountList.add(map.get("totalAmount"));
            payAmountList.add(map.get("payAmount"));
            discountAmountList.add(map.get("discountAmount"));
        }
        model.addAttribute("XList", JSON.toJSONString(XList));
        model.addAttribute("numList",JSON.toJSONString(numList));
        model.addAttribute("totalAmountList",JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmountList",JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmountList",JSON.toJSONString(discountAmountList));

        return "/businessReport/listBar";
    }
}
